/*
 * @Author: fisher
 * @Date: 2023-11-13 09:36:15
 * @LastEditTime: 2023-11-13 09:41:09
 * @LastEditors: fisher
 * @Description:
 * @FilePath: \FCT\src\utils\cesium.js
 */

/**
 * @description: 设置镜头操作
 * @param {Object} viewData
 * @param {Number} duration
 * @return {*}
 */
export const setView = (viewData, duration = 1.5) => {
  viewer.scene.camera.flyTo({
    destination: new Cesium.Cartesian3(viewData.x, viewData.y, viewData.z),
    orientation: {
      heading: viewData.heading,
      pitch: viewData.pitch,
      roll: viewData.roll,
    },
    duration: duration,
  });
};

/**
 * @description: 计算两坐标间距
 * @param {*} p1
 * @param {*} p2
 * @return {*}
 */
export const two_points_distance = (p1, p2) => {
  // 经纬度转换为世界坐标
  const start_position = Cesium.Cartesian3.fromDegrees(p1.x, p1.y, p1.z);
  const end_position = Cesium.Cartesian3.fromDegrees(p2.x, p2.y, p2.z);
  // 返回两个坐标的距离（单位：米）
  return Cesium.Cartesian3.distance(start_position, end_position);
};

/**
 * @description: 屏幕坐标转经纬度坐标
 * @param {*} position
 * @return {*}
 */
export const screen2degrees = (position) => {
  const res = viewer.scene.pickPosition(position);
  //将笛卡尔坐标转化为经纬度坐标
  const cartographic = Cesium.Cartographic.fromCartesian(res);
  const longitude = Cesium.Math.toDegrees(cartographic.longitude);
  const latitude = Cesium.Math.toDegrees(cartographic.latitude);
  const height = cartographic.height;
  return {
    x: longitude,
    y: latitude,
    z: height,
  };
};
